<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
if (!Session::has('DSL','admin')){
    die('非法登录');
}

class Modules extends Controller
{
    public function index(){
        $modules_list = db('modules')->paginate(10);
        $page = $modules_list->render();
        $this->assign('modules',$modules_list);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    
    public function add_module(){
        return $this->fetch('add');
    }
    
    public function edit_module(){
        $module_id = input('get.module_id');
        $module = model('Modules')->getModule($module_id);
        $submodules = db('submodules')->where('parent_id',$module_id)->paginate(10,false,['query'=>request()->param()]);
        $page = $submodules->render();
        $this->assign('module',$module);
        $this->assign('submodules',$submodules);
        $this->assign('page',$page);
        return $this->fetch('edit');
    }
    
    public function del_module()
    {
        $result = array('err' =>'1','content' => '');
        return $result;
    }
    
    public function add()
    {
        $result = array('err' => '1','content' => '');
        $data = input('post.');
        $rules = [
            'module' => 'require|max:20'
        ];
        $message = [
            'module.require' => '模块名必须',
            'module.max' => '模块名字数请控制在20以内！'
        ];
        $res1 = $this->validate($data, $rules,$message);
        if ($res1 != true){
            $result['content'] = $res1;
            return $result;
        }
        $rules2 = [
            'submodule' => 'require|max:20',
            'data_id' => 'unique:submodules',
            'url' => 'url'
        ];
        $message2 =[
            'submodule.max' => '子模块名的字数请控制在20以内！',
            'submodule.require' => '子模块名必填！',
            'data_id.require' => 'data-id字段必填',
            'url.url' => '请填写正确的url格式'
        ];
        if (!is_array($data['submodule'])){
            $result['content'] = '数据错误！';
            return $result;
        }
        foreach ($data['submodule'] as $k => $v){
            $res2 = $this->validate($v, $rules2,$message2);
            if ($res2 != true){
                $result['content'] = $res2;
                return $result;
            }
        }
        $model = model('Modules');
        $add_data = $model->addModules($data);
        if ($add_data == false){
            $result['content'] = '数据插入失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '添加成功！';
        return $result;
    }
    
    public function del_submodule()
    {
        $result = array('err' => '1','content' => "");
        $submodule_id = input('post.id');
        $del_submodule = db('submodules')->where('id',$submodule_id)->delete();
        if ($del_submodule == false){
            $result['content'] = '删除失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '删除成功！';
        return $result;
    }
    public function edit()
    {
        $result = array('err' =>'1','content' => '');
        $data = input('post.');
        $rules = [
            'id' => 'require|number',
            'module' =>'require|max:20',
        ];
        $message = [
            'id.require' => '请求错误！',
            'id.number' => '非法请求！',
            'module.require' => '模块名不能为空！',
            'module.max' =>'模块名字数请控制在20以内'
        ];
        $res = $this->validate($data, $rules,$message);
        if ($res != true){
            $result['content'] = $res;
            return $result;
        }
        $add_submodules = array();
        $count = count($data['submodules']);
        for ($i = 0;$i < $count;$i++){
            if ($data['submodules'][$i]['id'] == 'new'){
                unset($data['submodules'][$i]['id']);
                array_push($add_submodules, $data['submodules'][$i]);
            }
        }
        $rules1 = [
            'submodule' => 'require|max:20',
            'data_id' => 'require|max:20',
            'url' => 'require|url',
        ];
        $message1 = [
            'submodule.require' => '子模块名必须',
            'submodule.max' => '子模块名的字数控制在20以内',
            'data_id.require' => 'data-id必须',
            'data_id.max' => 'data-id字数请控制在20以内',
            'url.require' => 'url必须',
            'url.url' => '请正确填写url'
        ];
        $res1 = $this->validate($data['submodules'], $rules1,$message1);
        if ($res1 !=true){
            $result['content'] = $res1;
            return $result;
        }
        $rules2 = [
            'data_id' => 'unique:submodles',
        ];
        $message2 = [
            'data_id.unique' => 'data-id值必须唯一'
        ];
        $res2 = $this->validate($add_submodules, $rules2,$message2);
        if ($res2 != true){
            $result['content'] = $res2;
            return $result;
        }
        $model = model('Modules');
        $save_date = $model->saveSubmodules($data['submodules'],$data['id']);
        if ($save_date == false){
            $result['content'] = '保存数据失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '修改成功！';
        return $result;
    }
   
    
}